package com.wang.android.library.ui.dialog;

import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.v7.app.AlertDialog;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.wang.android.library.R;
import com.wang.android.library.tool.imageloader.WImageLoader;

/**
 * 类名：WNetLoadingDialog
 * 类描述：缓冲加载dialog
 * 创建人： WangHuanyu
 * 创建时间： 2017/5/24 9:28
 * 修改人：
 * 修改时间：
 */
public class WNetLoadingDialog extends AlertDialog {
    ImageView img;

    public WNetLoadingDialog(@NonNull Context context) {
        super(context);
    }

    public WNetLoadingDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    public WNetLoadingDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static WNetLoadingDialog show(Context context, boolean cancelable, OnCancelListener cancelListener) {
        WNetLoadingDialog dialog = new WNetLoadingDialog(context, cancelable, cancelListener);
        dialog.setOnKeyListener(new OnKeyListener()
        {
            @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
            {
                if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)
                {
                    dialog.cancel();
                }
                return false;
            }
        });
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        dialog.show();
        return dialog;
    }

    @Override protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.w_dialog_net_loading);
        WindowManager.LayoutParams lp;
        lp = getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.dimAmount = 0; // 去背景遮盖
        lp.alpha = 1.0f;
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        getWindow().setAttributes(lp);
        img = (ImageView) findViewById(R.id.img);
        WImageLoader.getInstance().loadResImage(getContext(),R.drawable.loading,img);
    }


}
